const app = getApp()
var template = require('../../../template/template');
const util = require('../../../utils/util');
var typeArrId = {};
var placeArrId = {};
var type_id;
var placeCur;
var adds = {};
var insert_id;
var is_submit = false;
var trd_session;
var punish_time;
var num;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeArr: ['请选择'],
    placeArr: ['请选择'],
    typeIndex: 0,
    placeIndex: 0,
    date: '',
    pics: []
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var tabnum = app.globalData.tabnum;
    template.tabbar("tabBar", 2, this, tabnum);//0表示第一个tabbar
    wx.getStorage({
      key: 'trd_session',
      success: function (res) {
        console.log(res.data);
        trd_session = res.data;
      },
    });
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    this.setData({
      date: Y + "-" + M + "-" + D,
    });
    punish_time = Y + "-" + M + "-" + D;

    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/Index/getTypeArr',
      data: '',
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        typeArrId = res.data.ids,
          that.setData({
            typeArr: res.data.names,
          });
        that.getPlaceList();
      }
    })
  },
  getPlaceList: function () {
    var that = this;
    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/index/getPlaceArr',
      data: { type_id: typeArrId[0] },
      method: 'get',
      header: {// 设置请求的 header  
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data);
        that.setData({
          placeArr: res.data.names
        });
        placeArrId = res.data.ids;
        placeCur = placeArrId[0];
        type_id = typeArrId[0];
      }
    });
  },
  onShow: function () {

  },
  addPic: function () {
    var that = this;
    if (this.data.pics.length < 9) {
      wx.chooseImage({
        count: 9, // 最多可以选择的图片张数，默认9
        sizeType: [ 'compressed'], // original 原图，compressed 压缩图，默认二者都有
        sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
        success: function (res) {
          var imgsrc = that.data.pics.concat(res.tempFilePaths);
          that.setData({
            pics: imgsrc
          });
        },
      })
    } else {
      wx.showToast({
        title: '最多上传九张图片',
        icon: 'loading',
        duration: 1000
      });
    }
  },

  formSubmit: function (e) {
    if (is_submit) return;
    is_submit = true;
    var id = e.target.id;
    adds = e.detail.value;
    adds.place_id = placeCur;
    adds.type_id = type_id;
    adds.punish_time = punish_time;
    adds.trd_session = trd_session;
    this.uploadimg();
  },

  uploadimg: function () {//这里触发图片上传的方法
    var that = this;
    if (this.data.pics.length == 0) {
      wx.showToast({
        title: '请选择至少一张图片',
        duration: 1000
      });
      is_submit = false;
      return;
    }
    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/Index/warnInterface',
      data: adds,
      method: 'POST',
      header: {// 设置请求的 header 
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        insert_id = res.data;
        that.upPics();
      }
    })
  },

  upPics: function () {
    var that = this;
    for (var i = 0; i < this.data.pics.length; i++) {
      num = i+1;
      wx.uploadFile({
        url: 'https://xcx.chenjiudenanren.com/inter/Index/warnImgUpload',
        filePath: that.data.pics[i],
        name: 'pic',
        formData: { id: insert_id },
        success: function (res) {
          if (res) {
            wx.showToast({
              title: '提交成功',
              duration: 1000
            });
          }
          if (num == res.data) {
            
            setTimeout(function () {
              wx.redirectTo({
                url: '../warn/warn'
              })
            }, 2000)
            is_submit = false;
          }
        }
      })
    }
    
  },

  bindTypeChange: function (e) {
    var that = this;
    this.setData({
      typeIndex: e.detail.value
    });
    type_id = typeArrId[e.detail.value];
    wx.request({
      url: 'https://xcx.chenjiudenanren.com/inter/index/getPlaceArr',
      data: { type_id: type_id },
      method: 'get',
      header: {// 设置请求的 header  
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res.data);
        that.setData({
          placeArr: res.data.names,
        });
        placeArrId = res.data.ids;
      }
    })
  },

  bindPlaceChange: function (e) {
    this.setData({
      placeIndex: e.detail.value,
    });
    console.log(e.detail.value);
    console.log(placeArrId);
    placeCur = placeArrId[e.detail.value];
  },

  bindDateChange: function (event) {
    //console.log(event.detail.value);
    this.setData({
      date: event.detail.value
    })
    punish_time = event.detail.value;
  }

})